package com.business_clean.ui.activity.camera;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.media.MediaPlayer;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.config.Constant;
import com.business_clean.app.ext.CommonUtils;

import com.business_clean.app.service.ServerReporter;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.AnimationUtils;
import com.business_clean.app.uitl.ImageCompressor;
import com.business_clean.app.uitl.Mp4Utils;
import com.business_clean.app.uitl.PhotoBitmapUtils;
import com.business_clean.app.uitl.RxHelper;
import com.business_clean.app.uitl.ShareUtils;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.app.uitl.share.ShareHelperTools;
import com.business_clean.app.uitl.share.ShareParams;
import com.business_clean.app.uitl.share.SocialBitmapUtils;
import com.business_clean.app.weight.dialog.PagerDrawerPopup;
import com.business_clean.data.initconfig.WaterMarkBitmapData;
import com.business_clean.data.mode.camera.LeaderClockEntity;
import com.business_clean.data.mode.camera.LeaderClockListEntity;
import com.business_clean.data.mode.camera.LeaderClockUserListEntity;
import com.business_clean.data.mode.leader.CustomMarkLeaderData;
import com.business_clean.data.mode.project.ProjectMangerList;
import com.business_clean.data.mode.roster.RosterList;
import com.business_clean.databinding.ActivityEditPhotoBinding;
import com.business_clean.ui.activity.personnel.SelectPersonnelActivity;
import com.business_clean.ui.adapter.camera.LeaderItemAdapter;
import com.business_clean.viewmodel.request.CameraViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.king.drawboard.draw.Draw;
import com.king.drawboard.view.DrawBoardView;
import com.luck.picture.lib.interfaces.OnKeyValueResultCallbackListener;
import com.luck.picture.lib.utils.DateUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;

import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.functions.Consumer;
import me.hgj.mvvmhelper.ext.AppExtKt;
import me.hgj.mvvmhelper.ext.LogExtKt;
import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;
import top.zibin.luban.OnRenameListener;

public class EditPhotoActivity extends BaseActivity<CameraViewModel, ActivityEditPhotoBinding> {

    private String TAG = "EditPhotoActivity";
    private Bitmap originalBitmap;//原始图片
    private Bitmap waterBitmap;//水印图片
    private Bitmap keyBitmap;//原始图片
    private int channel;//用来区分是图片/视频
    private String videoPath;

    public List<String> mp4List = new ArrayList<>();
    private String takePhotoMills;//时间戳

    private List<CustomMarkLeaderData> originalLeaderEntity;//记录所有的 uuid 出来

    private int act_type = 2;//来区分是日常工作还是领班打卡 默认是领班打卡

    private WaterMarkBitmapData waterMarkBitmapData;

    private LeaderItemAdapter mAdapter;
    private LeaderItemAdapter mAdapter1;

    private String class_uuid0;
    private String class_uuid1;

    private String class_time;
    private String class_time1;
    private String class_name1;
    private String class_name;

    private String uuids;
    private boolean isTopAdapter = true;//记录点击那个 adapter 然后 add 到某个里面

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        initIntent();
        fillProject();
        // 领班打卡需要的参数
        View footerView = View.inflate(this, R.layout.item_leader_clock_footview, null);
        //因为只有 2 个，先写死，后期优化
        mAdapter = new LeaderItemAdapter();
        mDatabind.recycler.setLayoutManager(new LinearLayoutManager(this));
        mDatabind.recycler.setAdapter(mAdapter);
        mAdapter.addFooterView(footerView);

        //1
        mAdapter1 = new LeaderItemAdapter();
        mDatabind.recycler1.setLayoutManager(new LinearLayoutManager(this));
        mDatabind.recycler1.setAdapter(mAdapter1);
        View footerView1 = View.inflate(this, R.layout.item_leader_clock_footview, null);
        mAdapter1.addFooterView(footerView1);
    }

    private void initIntent() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            channel = getIntent().getExtras().getInt("channel");
            takePhotoMills = getIntent().getExtras().getString("takePhotoMills");
            String act_type_string = getIntent().getExtras().getString("act_type");
            //1打卡 2领班打卡 3日常
            if ("日常".equals(act_type_string)) {
                act_type = 3;
                mDatabind.llScollview.setVisibility(View.INVISIBLE);
            } else if ("领班打卡".equals(act_type_string)) {
                act_type = 2;
            } else {
                act_type = 1;
            }
            LogUtils.e("我来编辑界面了 时间戳是： " + takePhotoMills);
            if (App.appViewModelInstance.getWaterMarkBitmap().getValue() != null) {
                waterMarkBitmapData = App.appViewModelInstance.getWaterMarkBitmap().getValue();
            }
            if (1 == channel) {//图片
                initPaintMode();
                if (waterMarkBitmapData != null) {
                    originalBitmap = waterMarkBitmapData.getOriginalBitmap();
                    mDatabind.drawView.setImageBitmap(waterMarkBitmapData.getOriginalBitmap());
                    waterBitmap = waterMarkBitmapData.getWaterBitmap();
                    keyBitmap = waterMarkBitmapData.getKeyBitmap();
                    mDatabind.drawView.setWatermarkImages(ImageUtils.scale(waterMarkBitmapData.getWaterBitmap(), 0.7f, 0.7f), waterMarkBitmapData.getKeyBitmap());
                }
                mDatabind.flPhotoLayout.setVisibility(View.VISIBLE);
                mDatabind.flVideoLayout.setVisibility(View.GONE);

            } else if (2 == channel) {//视频
                mDatabind.flPhotoLayout.setVisibility(View.GONE);
                mDatabind.flVideoLayout.setVisibility(View.VISIBLE);
                videoPath = getIntent().getExtras().getString("video_path");
                mDatabind.video.setVideoPath(videoPath);
                mDatabind.video.start();
                initVideoListener();
            } else {
                ToastUtil.show("未拿到任何数据");
            }
        }


        if (Constant.ROLE_MANGER || Constant.ROLE_HR) {
            mDatabind.ivChange.setVisibility(View.VISIBLE);
        } else {
            mDatabind.ivChange.setVisibility(View.GONE);
        }

    }

    private void initPaintMode() {
        mDatabind.drawView.setZoomEnabled(false);//是否启用缩放
        mDatabind.drawView.setLineStrokeWidth(8f);
        //绘图监听，绘制了后，再显示清除按钮
        mDatabind.drawView.setOnDrawListener(new DrawBoardView.OnDrawListener() {
            @Override
            public void onDraw(Draw draw) {
                if (mDatabind.llErase.getVisibility() == View.GONE) {
                    AnimationUtils.slideInFromLeft(mDatabind.llErase);
                }
            }
        });
    }

    /**
     * 监听播放器的方法
     */
    private void initVideoListener() {
        mDatabind.video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mDatabind.ivVideoStatus.setImageResource(R.mipmap.icon_camera_play);
            }
        });
    }

    @Override
    public void initObserver() {
        //切换项目
        App.appViewModelInstance.getProjectInfo().observe(this, new Observer<ProjectMangerList>() {
            @Override
            public void onChanged(ProjectMangerList projectMangerList) {
                fillProject();
            }
        });
//        //在这里 坚挺领班打卡的人员
//        App.appViewModelInstance.getLeaderData().observe(this, new Observer<LeaderClockEntity>() {
//            @Override
//            public void onChanged(LeaderClockEntity leaderClockEntity) {
//                leaderEntity = leaderClockEntity;
//            }
//        });

        //选择人员 回调，
        App.appViewModelInstance.getSelectedPersonnel().observe(this, new Observer<List<RosterList>>() {
            @Override
            public void onChanged(List<RosterList> rosterLists) {
                StringBuilder builderUUIDs = new StringBuilder();

                //选择好人员后，更新当前的班次列表
                for (RosterList item : rosterLists) {
                    if (item.isChoice()) {
                        builderUUIDs.append(item.getUuid()).append(",");

                        LeaderClockUserListEntity entity = new LeaderClockUserListEntity();
                        entity.setAvatar(item.getAvatar());
                        entity.setUser_name(item.getUser_name());
                        entity.setStatus_name("加班");
                        entity.setStatus("6");
                        entity.setUuid(item.getUuid());
                        entity.setJob_name(item.getJob_name());
                        entity.setProject_name(entity.getProject_name());
                        entity.setAdd(true);
                        if (isTopAdapter) {
                            mAdapter.getData().add(mAdapter.getData().size(), entity);
                        } else {
                            mAdapter1.getData().add(mAdapter1.getData().size(), entity);
                        }
                    }
                }
                if (isTopAdapter) {
                    mAdapter.notifyDataSetChanged();
                } else {
                    mAdapter1.notifyDataSetChanged();
                }

                if (builderUUIDs.length() > 0) {
                    builderUUIDs.deleteCharAt(builderUUIDs.length() - 1);
                }

                uuids = builderUUIDs.toString();
            }
        });
    }

    @Override
    public void onBindViewClick() {
        //清除图片上一步操作
        mDatabind.llErase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDatabind.drawView.clear();
                AnimationUtils.slideOutToRight(mDatabind.llErase);
            }
        });

        //底部增加人员
        mAdapter.getFooterLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isTopAdapter = true;
                Bundle bundle = new Bundle();
                bundle.putInt("multiple", 2);
                bundle.putInt("channel", 1);
                bundle.putString("class_uuid", "" + class_uuid0);
                bundle.putString("uuids", "" + uuids);
                ActivityForwardUtil.startActivity(SelectPersonnelActivity.class, bundle);
            }
        });
        //底部增加人员
        mAdapter1.getFooterLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isTopAdapter = false;
                Bundle bundle = new Bundle();
                bundle.putInt("multiple", 2);
                bundle.putInt("channel", 1);
                bundle.putString("class_uuid", "" + class_uuid0);
                ActivityForwardUtil.startActivity(SelectPersonnelActivity.class, bundle);
            }
        });


        //这里是 item 改动
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.tv_item_leader_item_jiaban) {//如果是加班的，不删除，但是隐藏掉状态修改
                    mAdapter.getData().remove(position);//删除某一个
                    mAdapter.notifyDataSetChanged();
                } else if (view.getId() == R.id.tv_item_leader_item_status) {
                    if (!mAdapter.getData().get(position).isAdd()) {//只有不是新增的才可以修改数据
                        showStatusDialog(mAdapter.getData().get(position), mAdapter);
                    }
                }
            }
        });


        //这里是 item 改动
        mAdapter1.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.tv_item_leader_item_jiaban) {//如果是加班的，不删除，但是隐藏掉状态修改
                    mAdapter1.getData().remove(position);//删除某一个
                    mAdapter1.notifyDataSetChanged();
                } else if (view.getId() == R.id.tv_item_leader_item_status) {
                    if (!mAdapter1.getData().get(position).isAdd()) {//只有不是新增的才可以修改数据
                        showStatusDialog(mAdapter1.getData().get(position), mAdapter1);
                    }
                }
            }
        });

        //视频的暂停播放
        mDatabind.ivVideoStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDatabind.video.isPlaying()) {
                    mDatabind.ivVideoStatus.setImageResource(R.mipmap.icon_camera_play);
                    mDatabind.video.pause();
                } else {
                    mDatabind.video.start();
                    mDatabind.ivVideoStatus.setImageResource(0);
                }
            }
        });
        //保存
        mDatabind.butBottomButNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断是否可以保存图片
                saveFile();
                AppExtKt.finishActivity(EditPhotoActivity.class);
            }
        });
        //分享到企业微信，并保存
        mDatabind.butBottomButLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (1 == channel) {//分享图片
                    Bitmap waterMarkBitmap = PhotoBitmapUtils.drawWaterToBitMap2(EditPhotoActivity.this,
                            waterBitmap, mDatabind.drawView.getResultBitmap(true), keyBitmap);
                    if (waterMarkBitmap != null) {
                        ShareParams params = new ShareParams();
                        params.setBitmap(waterMarkBitmap);
                        ShareHelperTools.getInstance().shareImage(params, EditPhotoActivity.this);
                    } else {
                        ToastUtil.show("无效的图片");
                    }
                } else {
                    shareLocalVideo();//分享本地视频
                }
                finish();
            }
        });

        //关闭界面
        mDatabind.backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //切换 项目
        mDatabind.llChangeProject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.ROLE_MANGER || Constant.ROLE_HR) {
                    new XPopup.Builder(EditPhotoActivity.this)
                            .asCustom(new PagerDrawerPopup(EditPhotoActivity.this)).show();
                }
            }
        });
    }


    /**
     * 分享视频出去
     */
    private void shareLocalVideo() {
        if (TextUtils.isEmpty(videoPath)) {
            return;
        }
        ShareUtils.shareWechatFriend(this, new File(videoPath));
    }

    //合成视频
    private void mergeMp4List() {
        String savePath = Constant.CAMERA_SAVE_PHOTO_VIDEO ? Constant.EXTERNAL_PHOTO_PATH : Constant.INTERNAL_VIDEO_PATH;
        Mp4Utils.mergeMp4List(mp4List, new File(savePath + "/" + System.currentTimeMillis() + "mergeVideo.mp4")).compose(RxHelper.rxSchedulerHelper()).subscribe(new Consumer<File>() {
            @Override
            public void accept(File mergeVideo) throws Exception {
                if (Constant.CAMERA_SAVE_PHOTO_VIDEO) {
                    Mp4Utils.saveVideoToAlbum(EditPhotoActivity.this, mergeVideo.getAbsolutePath());
                    FileUtils.notifySystemToScan(mergeVideo);
                }
                mp4List.clear();
                LogExtKt.logE("合并了完成了", TAG);
            }
        }, throwable -> {
            throwable.printStackTrace();
            LogExtKt.logE("合并出现异常" + throwable.getMessage(), TAG);
        });
    }


    private void fillProject() {
        if (App.appViewModelInstance.getProjectInfo().getValue() != null) {
            ProjectMangerList mangerList = App.appViewModelInstance.getProjectInfo().getValue();
            getMDatabind().tvRight.setText(mangerList.getProject_short_name());
        }
        if (act_type != 3) {
            //请求当前考勤的
            mViewModel.requestAttendance();
        }

    }


    /**
     * 因为保存跟分享都会走这个方法，所以封装起来了
     *
     * @return
     */
    private void saveFile() {
        if (1 == channel) {
            //转换水印照片
            Bitmap waterMarkBitmap = PhotoBitmapUtils.drawWaterToBitMap2(this, waterBitmap, mDatabind.drawView.getResultBitmap(true), keyBitmap);

            //先存到本地项目
            File file = saveBitmapToFile(waterMarkBitmap, Constant.INTERNAL_PHOTO_PATH, System.currentTimeMillis() + ".jpg");//水印拿到一个暂存的路径
            File thumbFile = saveBitmapToFile(PhotoBitmapUtils.getBitmapThumb(file.getAbsolutePath(), 200), Constant.INTERNAL_PHOTO_PATH, System.currentTimeMillis() + "thumb" + ".jpg");
            File originaFile = saveBitmapToFile(originalBitmap, Constant.INTERNAL_PHOTO_PATH, System.currentTimeMillis() + "origina" + ".jpg");//原图拿到一个暂存的路径

            //先压缩水印图片
            ImageCompressor.compressBitmap(this, file.getAbsolutePath(), 10, new OnKeyValueResultCallbackListener() {
                @Override
                public void onCallback(String srcPath, String resultWaterPath) {
                    //原图的压缩 压缩完成后 再去上传服务器，因为服务器要原图跟水印图，所以都需要压缩后 处理
                    ImageCompressor.compressBitmap(EditPhotoActivity.this,
                            originaFile.getAbsolutePath(), 10, new OnKeyValueResultCallbackListener() {
                                @Override
                                public void onCallback(String srcPath, String resultPath) {
                                    //原图的压缩 压缩完成后 再去上传服务器，因为服务器要原图跟水印图，所以都需要压缩后 处理
                                    ImageCompressor.compressBitmap(EditPhotoActivity.this,
                                            thumbFile.getAbsolutePath(), 10, new OnKeyValueResultCallbackListener() {
                                                @Override
                                                public void onCallback(String srcPath, String thumbPath) {
                                                    LogUtils.e("压缩完成后的图片 resultPath -->" + resultPath);
                                                    //拿到压缩后的路径了，那么就进行存储
                                                    if (Constant.CAMERA_SAVE_PHOTO_VIDEO) {//开启本地照片存储 需要保存水印照片
                                                        ImageUtils.save2Album(BitmapFactory.decodeFile(resultWaterPath),
                                                                Constant.EXTERNAL_PHOTO_PATH, Bitmap.CompressFormat.JPEG);
                                                        //是否需要保存原始图片到相册
                                                        if (Constant.CAMERA_SAVE_ORIGINAL_PHOTO) {
                                                            ImageUtils.save2Album(BitmapFactory.decodeFile(resultPath), Bitmap.CompressFormat.JPEG);
                                                        }
                                                    }
                                                    uploadMedia(1, resultWaterPath, resultPath, thumbPath);
                                                }
                                            });
                                }
                            });
                }
            });
        } else if (2 == channel) {//视频类型
            Bitmap thumb = PhotoBitmapUtils.getVideoThumbnail(videoPath, 90, 160, MediaStore.Video.Thumbnails.MINI_KIND);

            File thumbFile = saveBitmapToFile(thumb, Constant.INTERNAL_PHOTO_PATH, System.currentTimeMillis() + "video_thumb" + ".jpg");
            ImageCompressor.compressBitmap(EditPhotoActivity.this, thumbFile.getAbsolutePath(), 10,
                    new OnKeyValueResultCallbackListener() {
                        @Override
                        public void onCallback(String srcPath, String thumbPath) {
                            LogUtils.e("压缩完成后的图片 resultPath -->" + thumbPath);
                            //保存视频
                            mp4List.add(videoPath);
                            mergeMp4List();
                            uploadMedia(2, videoPath, null, thumbPath);
                        }
                    });
        }
    }


    //图片转换成File文件
    private File saveBitmapToFile(Bitmap waterMarkBitmap, String savePath, String finalFileName) {
        File file = new File(savePath + finalFileName);
        ImageUtils.save(waterMarkBitmap, savePath + finalFileName, Bitmap.CompressFormat.JPEG);
        return file;
    }

    /**
     * 上传图片的操作
     * message_type 区分是图片还是视频
     * file 水印的
     * original 无水印的
     */
    private void uploadMedia(int message_type, String file, String original, String thumb) {
        if (waterMarkBitmapData == null) {
            return;
        }
        if (TextUtils.isEmpty(takePhotoMills)) {
            takePhotoMills = String.valueOf(0);
        }


        //这是要把元数据的内容转成 data
        List<CustomMarkLeaderData> attendance_data_check = covertList(getAdapterData());

        //存储不一样的
        List<CustomMarkLeaderData> cancel_overtime_data = new ArrayList<>();

        //originalLeaderEntity 是服务器所有的 uuid 组合
        if (originalLeaderEntity != null) {
            for (CustomMarkLeaderData markLeaderData : originalLeaderEntity) {
                for (CustomMarkLeaderData data : attendance_data_check) {
                    if (!markLeaderData.getUuid().equals(data.getUuid()) && "6".equals(data.getStatus())) {
                        cancel_overtime_data.add(data);
                    }
                }
            }
        }

//        List<CustomMarkLeaderData> attendance_data = new ArrayList<>();

        //这是正常统计上班人员的统计
//        if (leaderEntity != null && leaderEntity.getClass_list() != null && act_type == 2) {//必须是邻班打卡才来选择人员
//            List<LeaderClockListEntity> leaderClockEntities = leaderEntity.getClass_list();
//            for (LeaderClockListEntity list : leaderClockEntities) {
//                String classUuid = list.getUuid(); // 班次的 uuid
//                for (LeaderClockUserListEntity list1 : list.getUser_list()) {
//                    CustomMarkLeaderData markLeaderData = new CustomMarkLeaderData();
//                    markLeaderData.setClass_uuid(classUuid);
//                    markLeaderData.setStatus(list1.getStatus()); // 人员状态
//                    markLeaderData.setUuid(list1.getUuid()); // 人员uuid
//                    markLeaderData.setUser_name(list1.getUser_name()); // 人员名称
//                    attendance_data.add(markLeaderData);
//                }
//            }
//        }
        ServerReporter.getInstance().reportToServer(act_type, message_type, waterMarkBitmapData.getLat(),
                waterMarkBitmapData.getLnt(), waterMarkBitmapData.getAddress(), file, original, thumb,
                attendance_data_check, cancel_overtime_data, Long.parseLong(takePhotoMills));
        App.appViewModelInstance.getWaterMarkBitmap().setValue(null);
    }

    private LeaderClockEntity getAdapterData() {
        //保存到公共的，然后传递给上层 如果有多个组合起来
        LeaderClockEntity leaderClock = new LeaderClockEntity();
        List<LeaderClockListEntity> listEntities = new ArrayList<>();
        //班次 1
        LeaderClockListEntity leaderClockListEntity = new LeaderClockListEntity();
        leaderClockListEntity.setClass_name(class_name);
        leaderClockListEntity.setClass_time(class_time);
        leaderClockListEntity.setUuid(class_uuid0);

        leaderClockListEntity.setUser_list(mAdapter.getData());
        //班次 2
        LeaderClockListEntity leaderClockListEntity1 = new LeaderClockListEntity();
        leaderClockListEntity1.setClass_name(class_name1);
        leaderClockListEntity1.setClass_time(class_time1);
        leaderClockListEntity1.setUuid(class_uuid1);
        leaderClockListEntity1.setUser_list(mAdapter1.getData());
        //增加到 list
        listEntities.add(leaderClockListEntity);
        listEntities.add(leaderClockListEntity1);
        //内容
        leaderClock.setClass_list(listEntities);
        return leaderClock;
    }


    /**
     * 来做考勤的转换
     *
     * @param leaderClock
     */
    private List<CustomMarkLeaderData> covertList(LeaderClockEntity leaderClock) {
        List<CustomMarkLeaderData> attendance_data = new ArrayList<>();
        List<LeaderClockListEntity> leaderClockEntities = leaderClock.getClass_list();
        for (LeaderClockListEntity list : leaderClockEntities) {
            String classUuid = list.getUuid(); // 班次的 uuid
            for (LeaderClockUserListEntity list1 : list.getUser_list()) {
                CustomMarkLeaderData markLeaderData = new CustomMarkLeaderData();
                markLeaderData.setClass_uuid(classUuid);
                markLeaderData.setStatus(list1.getStatus()); // 人员状态
                markLeaderData.setUuid(list1.getUuid()); // 人员uuid
                markLeaderData.setUser_name(list1.getUser_name()); // 人员名称
                attendance_data.add(markLeaderData);
            }
        }
        return attendance_data;
    }


    @Override
    public void onRequestSuccess() {
        //考勤统计
        mViewModel.getLeaderClockEntity().observe(this, new Observer<LeaderClockEntity>() {
            @Override
            public void onChanged(LeaderClockEntity leaderClockEntity) {
                originalLeaderEntity = covertList(leaderClockEntity);
                fillData(leaderClockEntity);
            }
        });
    }


    public void fillData(LeaderClockEntity leaderClockEntity) {
        mDatabind.leaderEmpty.llEmpty.setVisibility(View.GONE);//先隐藏

        class_uuid0 = "";
        class_name = "";
        class_time = "";
        mAdapter.setList(null);
        class_uuid1 = "";
        class_name1 = "";
        class_time1 = "";
        mAdapter1.setList(null);

        if (leaderClockEntity.getClass_list().size() >= 1) {
            mDatabind.tvClassName0.setVisibility(View.VISIBLE);
            mDatabind.tvClassName0.setText(leaderClockEntity.getClass_list().get(0).getClass_name());
            class_uuid0 = leaderClockEntity.getClass_list().get(0).getUuid();
            class_name = leaderClockEntity.getClass_list().get(0).getClass_name();
            class_time = leaderClockEntity.getClass_list().get(0).getClass_time();
            mAdapter.setList(leaderClockEntity.getClass_list().get(0).getUser_list());
        }
        if (leaderClockEntity.getClass_list().size() >= 2) {
            mDatabind.tvClassName1.setVisibility(View.VISIBLE);
            mDatabind.recycler1.setVisibility(View.VISIBLE);
            mDatabind.tvClassName1.setText(leaderClockEntity.getClass_list().get(1).getClass_name());
            class_uuid1 = leaderClockEntity.getClass_list().get(1).getUuid();
            class_name1 = leaderClockEntity.getClass_list().get(1).getClass_name();
            class_time1 = leaderClockEntity.getClass_list().get(1).getClass_time();
            mAdapter1.setList(leaderClockEntity.getClass_list().get(1).getUser_list());
        }
        if (mAdapter.getData().size() == 0) {
            mDatabind.viewLine.setVisibility(View.GONE);
        }

        if (mAdapter.getData().size() == 0 && mAdapter1.getData().size() == 0 && TextUtils.isEmpty(class_uuid0) && TextUtils.isEmpty(class_uuid1)) {
            mDatabind.leaderEmpty.llEmpty.setVisibility(View.GONE);
        }
    }


    /**
     * 更新 item 的数据
     */
    private void showStatusDialog(LeaderClockUserListEntity data, LeaderItemAdapter leaderItemAdapter) {
        String[] strings = {"改为休假", "改为出勤", "改为旷工", "改为缺卡"};
        CommonUtils.showBottomListWith(this, 20, "请选择出勤情况", strings, new OnSelectListener() {
            @Override
            public void onSelect(int position, String text) {
                String status = "";//1休假 2正常出勤  4旷工 5休息 6加班
                String statusName = "";
                switch (position) {
                    case 0:
                        status = "1";
                        statusName = "休假";
                        break;
                    case 1:
                        status = "2";
                        statusName = "出勤";
                        break;
                    case 2:
                        status = "4";
                        statusName = "旷工";
                        break;
                    case 3:
                        status = "3";
                        statusName = "缺卡";
                        break;
                }
                data.setStatus(status);
                data.setStatus_name(statusName);
                if (leaderItemAdapter != null) {
                    leaderItemAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mDatabind.video.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDatabind.video.pause();
    }


    @Override
    protected void onStop() {
        super.onStop();
        mDatabind.video.stopPlayback();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (originalBitmap != null && !originalBitmap.isRecycled()) {
            originalBitmap.recycle();
        }
        if (waterBitmap != null && !waterBitmap.isRecycled()) {
            waterBitmap.recycle();
        }
        if (keyBitmap != null && !keyBitmap.isRecycled()) {
            keyBitmap.recycle();
        }
        App.appViewModelInstance.getWaterMarkBitmap().setValue(null);
        mDatabind.video.suspend();
    }
}